package com.vip.jwt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vip.jwt.entity.Menu;
import com.vip.jwt.entity.RoleMenu;
import com.vip.jwt.mapper.MenuMapper;
import com.vip.jwt.mapper.RoleMenuMapper;
import com.vip.jwt.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色菜单关联表 服务实现类
 * </p>
 *
 * @author vip
 * @since 2021-05-04
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private MenuMapper menuMapper;

    @Override
    public int deleteByRoleId(String roleId) {
        QueryWrapper<RoleMenu> roleMenuQueryWrapper = new QueryWrapper<>();
        roleMenuQueryWrapper.eq("role_id",roleId);
        return roleMenuMapper.delete(roleMenuQueryWrapper);
    }

    @Override
    public List<String>[] findByRoleId(String roleId) {
        QueryWrapper<RoleMenu> roleMenuQueryWrapper = new QueryWrapper<>();
        roleMenuQueryWrapper.eq("role_id",roleId);
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(roleMenuQueryWrapper);
        List<Menu> menus = menuMapper.selectList(null);
        return RoleMenu.findByIds(menus, roleMenus);
    }

}
